package com.example.nextstream.service.impl;

import com.example.nextstream.service.SessionStore;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.net.GuacamoleTunnel;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service("sessionStore")
public class SessionStoreImpl implements SessionStore {
    private Map<String, GuacamoleTunnel> sessionStore= new HashMap<String, GuacamoleTunnel>();

    @Override
    public void setSessionObj(String sessionId, GuacamoleTunnel obj){
       // sessionStore.remove(sessionId);
        try{
            sessionStore.get(sessionId).close();
        }catch (GuacamoleException e){

        }catch(NullPointerException e){}
        sessionStore.put(sessionId,obj);
    }

    @Override
    public GuacamoleTunnel getSessionObj(String sessionId) {
        try{
            GuacamoleTunnel tunnel =sessionStore.get(sessionId);
            return tunnel;
        }catch (NullPointerException e){
            return null;
        }
    }

    @Override
    public void removeSessionObj(String sessionId) {
        try{
            var session =  sessionStore.get(sessionId);
            session.close();
            //session.super.finalize();

        }catch (GuacamoleException e){

        }catch(NullPointerException e){}
        sessionStore.remove(sessionId);
    }

    @Override
    public Boolean isSessionObj(String sessionId) {
        return sessionStore.containsKey(sessionId);
    }
}
